﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Security.Permissions;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace OSGeo.MapGuide.UI
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class MgFeatureSource : DataSourceControl
    {
        public MgFeatureSource()
        {
            _view = new MgFeatureSourceView(this, _views[0]);
        }

        protected override void OnLoad(EventArgs e)
        {
            _view.InitMapGuideSession();
            base.OnLoad(e);
        }

        [Browsable(true)]
        [Description("The MapGuide Feature Source ID")]
        [Category("Feature Source")]
        [DefaultValue("")]
        public string FeatureSourceID
        {
            get
            {
                object s = ViewState["FeatureSourceID"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, FeatureSourceID, StringComparison.Ordinal) != 0)
                {
                    ViewState["FeatureSourceID"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("The Feature Source Schema")]
        [Category("Feature Source")]
        [DefaultValue("")]
        public string SchemaName
        {
            get
            {
                object s = ViewState["SchemaName"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, SchemaName, StringComparison.Ordinal) != 0)
                {
                    ViewState["SchemaName"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("The Feature Source Class")]
        [Category("Feature Source")]
        [DefaultValue("")]
        public string ClassName
        {
            get
            {
                object s = ViewState["ClassName"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, ClassName, StringComparison.Ordinal) != 0)
                {
                    ViewState["ClassName"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        private ParameterCollection _selectParameters;

        public ParameterCollection SelectParameters
        {
            get
            {
                if (_selectParameters == null)
                {
                    _selectParameters = new ParameterCollection();
                }
                return _selectParameters;
            }
        }

        [Browsable(true)]
        [Description("The optional filter to apply when selecting features. If empty, all features are returned")]
        [Category("Select Command")]
        [DefaultValue("")]
        public string SelectFilter
        {
            get
            {
                object s = ViewState["SelectFilter"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, SelectFilter, StringComparison.Ordinal) != 0)
                {
                    ViewState["SelectFilter"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("Enable updates on this data source.")]
        [Category("Update Command")]
        [DefaultValue("")]
        public bool UpdateEnabled
        {
            get
            {
                object s = ViewState["UpdateEnabled"];
                return (s != null) ? (bool)s : false;
            }
            set
            {
                if (value != UpdateEnabled)
                {
                    ViewState["UpdateEnabled"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("Enable deletes on this data source.")]
        [Category("Delete Command")]
        [DefaultValue("")]
        public bool DeleteEnabled
        {
            get
            {
                object s = ViewState["DeleteEnabled"];
                return (s != null) ? (bool)s : false;
            }
            set
            {
                if (value != DeleteEnabled)
                {
                    ViewState["DeleteEnabled"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("Enable transactions on insert/update/delete operations. Only applies if underlying FDO provider supports transactions")]
        [Category("Transactions")]
        [DefaultValue(false)]
        public bool UseTransactions
        {
            get
            {
                object s = ViewState["UseTransactions"];
                return (s != null) ? (bool)s : false;
            }
            set
            {
                if (value != UseTransactions)
                {
                    ViewState["UseTransactions"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("Determines the source of the MapGuide session parameter. If manual, you must set the MapGuide session parameter manually via SetSessionID()")]
        [Category("MapGuide General")]
        [DefaultValue("")]
        public SessionParameterSource MapGuideSessionSource
        {
            get
            {
                object s = ViewState["MapGuideSessionSource"];
                return (s != null) ? (SessionParameterSource)s : SessionParameterSource.Manual;
            }
            set
            {
                if (MapGuideSessionSource != value)
                {
                    ViewState["MapGuideSessionSource"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("The name of the mapguide session parameter. The control will fetch this named parameter from the specified session source. If the session source is manual, nothing happens")]
        [Category("MapGuide General")]
        [DefaultValue("")]
        public string MapGuideSessionName
        {
            get
            {
                object s = ViewState["MapGuideSessionName"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, MapGuideSessionName, StringComparison.Ordinal) != 0)
                {
                    ViewState["MapGuideSessionName"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(true)]
        [Description("This string to display for null values")]
        [Category("Select Command")]
        [DefaultValue("")]
        public string NullValueString
        {
            get
            {
                object s = ViewState["NullValueString"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, NullValueString, StringComparison.Ordinal) != 0)
                {
                    ViewState["NullValueString"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        [Browsable(false)]
        public string SessionID
        {
            get
            {
                object s = ViewState["SessionID"];
                return (s != null) ? (string)s : String.Empty;
            }
            set
            {
                if (String.Compare(value, SessionID, StringComparison.Ordinal) != 0)
                {
                    ViewState["SessionID"] = value;
                    _view.RaiseChangedEvent();
                }
            }
        }

        protected static readonly string[] _views = { "DefaultView" };

        protected override DataSourceView GetView(string viewName)
        {
            if (!string.IsNullOrEmpty(viewName))
            {
                if (Array.IndexOf<string>(_views, viewName) < 0)
                {
                    throw new ArgumentException("An invalid view as requested", "viewName");
                }
            }
            return View;
        }

        protected override System.Collections.ICollection GetViewNames()
        {
            return _views;
        }

        protected MgFeatureSourceView _view;

        protected MgFeatureSourceView View
        {
            get
            {
                if (null == _view)
                {
                    _view = new MgFeatureSourceView(this, _views[0]);
                    if (base.IsTrackingViewState)
                    {
                        ((IStateManager)_view).TrackViewState();
                    }
                }
                return _view;
            }
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
        }

        protected override object SaveViewState()
        {
            return base.SaveViewState();
        }

        protected override void TrackViewState()
        {
            base.TrackViewState();
        }
    }

    public enum SessionParameterSource
    {
        Session,
        Post,
        Get,
        Manual
    }
}
